#include <windows.h>
#include<stdio.h>
#include<conio.h>
#include "resource.h"
#pragma hdrstop
HWND g_hDialog=NULL;
char sir[100];
HWND g_hdialog=NULL;
char qwe[10];
char subsir[50];
char S[100];
char s[100];
HWND hWnd2;
HWND hWnd3;
HWND hWnd4;
char text[100];
int ok=0;
int nr[100];
HWND hWnd5;
int alg=0;
int bo=0;
int aldoilea;
LPRECT *rt;
HWND hWnd6;
HWND hWnd;
HWND hWnd_p;
char chg[20];
char moore[5];
HDC hdc;
int p=112;
int n;
char procentaj[5];
char alg2[5];
int prim;
HWND g_hDialog2=NULL;
HWND g_hDialog4=NULL;
HWND g_hDialog5=NULL;
HWND perform=NULL;
int ex;
void schimba_moore()
{
	int i=0;
	while(ex)
	{
		moore[i]=ex%10+'0';
		i++;
		ex=ex/10;
	}
	moore[i]=NULL;
	strrev(moore);
}
void schimba()
{
	int i,k=0;
	for(i=0;i<strlen(text);i++)
	{
		chg[k]=nr[i]+'0';
		k++;
	}
	chg[k]=NULL;
}
void creeare_nr()
{
	int m,n,q,i,j,sum;
	n=strlen(text);
	m=strlen(subsir);
	q=0;
	for(i=0;i<n;i++)
	{	sum=1;
		if (m==1)
		{
			nr[q]=1;
			q++;
		}
		else
		{
			for(j=m-2;j>=0;j--)
			{
				if(text[i]==subsir[j])
				{
					nr[q]=sum;
					q++;
					break;
				}
				else
					sum++;
				if (sum==strlen(subsir))
				{
					nr[q]=sum;
					q++;
				}
			}
		}
	}
}

void creeare_txt()
{
	int i,j,ok,n,q=1;
	n=strlen(sir);
	text[0]=sir[0];
	for(i=1;i<n;i++)
	{
		ok=0;
		for(j=0;j<i;j++)
			if (sir[i]==sir[j])
			ok=1;
		if (ok==0)
		{
			text[q]=sir[i];
			q++;
		}
	}
	text[q]=NULL;
}
void alg_normal()
{
	
	int i,m=0,ok,j;
	int q=0;
	int numar=0;
	alg=0;
	int crt=strlen(s);
	int y=0;
	y=crt;
	p=45;
	while(m!=1 && strlen(s)<=strlen(S))
	{
		ok=0;
		i=strlen(s)-1;
		crt=y;
		while(crt)
		{
			alg=alg+1;
			if (s[i]!=S[i])
			{
				ok=1;
				break;
			}
			crt=crt-1;
			i--;
		}
		if (ok==1)
		{
			strrev(s);
			strcat(s," ");
			strrev(s);
		}
		else 
			m=1;
	}
	aldoilea=alg;
}

void procent(int proc)
{
	int i=0;
	if (proc!=0)
	{
	while(proc)
	{
		procentaj[i]=proc%10+'0';
		i++;
		proc=proc/10;
	}
	procentaj[i]=NULL;
	strrev(procentaj);
	char e[3]="%";
	strcat(procentaj,e);
	}
	else
	{
		procentaj[0]='0';
		procentaj[1]='%';
		procentaj[2]=NULL;
	}
}
void schimba_alg()
{
	int i=0;
	while(alg)
	{
		alg2[i]=alg%10+'0';
		i++;
		alg=alg/10;
	}
	alg2[i]=NULL;
	strrev(alg2);
}

PAINTSTRUCT ps;
void DrawTable(HDC hdc)
{
	int i;
	n=strlen(text);
	rt=(LPRECT*)malloc(sizeof(LPRECT)*n);
	for(i=0;i<n;i++)
		rt[i]=(LPRECT)malloc(sizeof(RECT));
	int aux=100;
	for(i=0;i<n;i++)
	{
		rt[i]->left=aux;
			rt[i]->top=153;
		rt[i]->right=aux+30;
			rt[i]->bottom=251;
		aux=aux+30;
	}
}
void afisare_sir(int m)
{
	int i,y;
	char c[5];
	y=0;
	for(i=0;i<strlen(sir);i++)
	{
		c[0]=sir[i];
		c[1]=NULL;
		TextOut(hdc,770+y,50+m*45,c,1);
		y=y+10;
	}
}
void afisare_subsir(int m)
{
	int i,y;
	char c[5];
	y=0;
	for(i=0;i<strlen(subsir);i++)
	{
		c[0]=subsir[i];
		c[1]=NULL;
		TextOut(hdc,770+y,65+m*45,c,1);
		y=y+10;
	}
}

void afisare_pas(int m,int n,char t)
{
	char s[10]="PASUL ";
	char c[5];
	c[0]=m+'0';
	c[1]=NULL;
	if(m>9)
	{
		c[0]='1';
		c[1]=m%10+'0';
		c[2]=NULL;
	}
	strcat(s,c);
	TextOut(hdc,690,65+m*45,s,strlen(s));
	TextOut(hdc,50,280+m*20,s,strlen(s));
	char x[100]="DEPLASAREA SUBSIRULUI";
	TextOut(hdc,150,260,x,strlen(x));
	char rt[100]=":subsirul se deplaseaza cu pasul caracterului***** ";
	char w[10];
	if (t==' ')
	{
		w[0]='s';
		w[1]='p';
		w[2]='a';
		w[3]='c';
		w[4]='e';
		w[5]=NULL;
	}
	else
	{
		w[0]=t;
		w[1]=NULL;
	}
	strcat(rt,w);
	TextOut(hdc,120,280+m*20,rt,strlen(rt));
}

void colorare(HDC hdc ,	PAINTSTRUCT ps,int R,int G,int B,int left,int top,int right,int bottom)
{
	HPEN yellowPen;
	HGDIOBJ  original;
			 original = NULL;
	yellowPen = CreatePen(PS_INSIDEFRAME,26,RGB(R,G,B));
			   SelectObject(ps.hdc, original);
			   SelectObject(ps.hdc,yellowPen);
			   Rectangle(hdc,left,top,right,bottom);
			     DeleteObject(yellowPen);
			   SelectObject(ps.hdc, original);
}

void boyer()
{
	int i,m=0,ok,j;
	int q=0;
	int numar=0;
	int bo=0;
	int crt=strlen(subsir);
	int y=0;
	y=crt;
	p=45;
	while(m!=1 && strlen(subsir)<=strlen(sir))
	{
		ok=0;
		i=strlen(subsir)-1;
		crt=y;
		while(crt)
		{
			bo=bo+1;
			if (subsir[i]!=sir[i])
			{
				ok=1;
				break;
			}
			crt=crt-1;
			i--;
		}
		if (ok==1)
		{
			strrev(subsir);
		char c;
		c=sir[strlen(subsir)-1];
		for(j=0;j<strlen(text);j++)
			if(text[j]==c)
			{
				q=nr[j];
				break;
			}
		for(i=0;i<q;i++)
			strcat(subsir," ");
		strrev(subsir);
		numar=numar+1;
		afisare_pas(numar,q-1,c);
		afisare_sir(numar);
		afisare_subsir(numar);
		p=p+45;
		}
		else
			m=1;
	}
	if(strlen(sir)<strlen(subsir))
		MessageBox(NULL,"SUBSIRUL CAUTAT NU A FOST GASIT","APPLICATION", NULL);
	else
		MessageBox(NULL,"SUBSIRUL CAUTAT A FOST GASIT","APPLICATION", NULL);
	ex=bo;
	prim=ex;
}

BOOL CALLBACK AboutDlgProc(HWND hWnd4, UINT Message, WPARAM wParam, LPARAM lParam)
{
    switch(Message)
    {
	case WM_COMMAND:
            switch(LOWORD(wParam))
            {
			case IDOK2:
                    EndDialog(hWnd4, IDOK);
					break;
            }
	case WM_DESTROY:
			EndDialog(hWnd4,0);
        break;
        break;
	default:
            return FALSE;
    }
    return TRUE;
}


BOOL CALLBACK AboutDlgPerform(HWND hWnd_p, UINT Message, WPARAM wParam, LPARAM lParam)
{
    switch(Message)
    {
		case WM_CLOSE:
				EndDialog(hWnd_p,0);
			break; 
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case IDDOK:
                    EndDialog(hWnd_p, IDOK);
                break;
				case IDD_AFISEAZA:
					{
						alg_normal();
						schimba_alg();
						schimba_moore();
						int proc=0;
						proc=100-100*prim/aldoilea;
						procent(proc);
						SetDlgItemText(perform,IDD_TEXT_ALG,alg2);
						SetDlgItemText(perform,IDD_TEXT_BOYER,moore);
						SetDlgItemText(perform,IDD_TEXT_PERFORM,procentaj);
						for(int y=0;y<strlen(moore);y++)
						{
							moore[y]=' ';
						}
						break;
					}
					UpdateWindow(hWnd_p);
            }
        break;
        default:
            return FALSE;
    }
    return TRUE;
}
BOOL CALLBACK AboutDlgProc5(HWND hWnd5, UINT Message, WPARAM wParam, LPARAM lParam)
{
    switch(Message)
    {
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case IDOK4:
                    EndDialog(hWnd5, IDOK);
                break;
            }
        break;
        default:
            return FALSE;
    }
    return TRUE;
}
BOOL CALLBACK AboutDlgProc6(HWND hWnd6, UINT Message, WPARAM wParam, LPARAM lParam)
{
    switch(Message)
    {
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case IDOK5:
                    EndDialog(hWnd6, IDOK);
                break;
            }
        break;
        default:
            return FALSE;
    }
    return TRUE;
}
BOOL CALLBACK DialogProc2( HWND hWnd3, UINT msg, WPARAM wParam, LPARAM lParam )
{
 switch(msg)  
  {  
	case WM_COMMAND:
		{
				switch(LOWORD(wParam))
				{
				case GET_TEXT:
					creeare_txt();
					SetDlgItemText(g_hdialog,EDIT4,text);
					break;
				case GET_NR:
					creeare_nr();
					schimba();
					SetDlgItemText(g_hdialog,IDC_EDIT2,chg);
					break;
				case IDC_NEXT:
					EndDialog(g_hdialog,0);
					//creeare tabel//
					InvalidateRect(hWnd2,NULL,TRUE);
					ok=1;
				}
				break;
		}
	case WM_CLOSE:
		EndDialog(g_hdialog,0);
			break; 
	default: return FALSE;
	}
  return TRUE;
}

//--------------------------------------------------//
//---------------------------------------------------------------------------

#pragma argsused
//---------------------------------------------------------------------------
LPCTSTR ClsName ="FundApp";
LPCTSTR WndName ="Project";
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg,
			      WPARAM wParam, LPARAM lParam);
//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nCmdShow)
{
	MSG        Msg;
	HWND       hWnd;
	WNDCLASSEX WndClsEx;

	// Create the application window
	WndClsEx.cbSize        = sizeof(WNDCLASSEX);
	WndClsEx.style         = CS_HREDRAW | CS_VREDRAW;
	WndClsEx.lpfnWndProc   = WndProcedure;
	WndClsEx.cbClsExtra    = 0;
	WndClsEx.cbWndExtra    = 0;
	WndClsEx.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
	WndClsEx.hCursor       = LoadCursor(NULL, IDC_ARROW);
	WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	WndClsEx.lpszMenuName  = MAKEINTRESOURCE(IDR_MENU1);
	WndClsEx.lpszClassName = ClsName;
	WndClsEx.hInstance     = hInstance;
	WndClsEx.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

	// Register the application
	RegisterClassEx(&WndClsEx);

	// Create the window object
	hWnd = CreateWindowEx(0,
                          ClsName,
                          WndName,
               			  WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT,
               			  CW_USEDEFAULT,
                          1350,
               			  700,
                          NULL,
                          NULL,
                          hInstance,
                          NULL);
	
	// Find out if the window was created
	if( !hWnd ) // If the window was not created,
		return FALSE; // stop the application

	// Display the window to the user
	ShowWindow(hWnd, nCmdShow);// SW_SHOWNORMAL);
	UpdateWindow(hWnd);

	// Decode and treat the messages
	// as long as the application is running
	while( GetMessage(&Msg, NULL, 0, 0) )
	{
             TranslateMessage(&Msg);
             DispatchMessage(&Msg);
	}

	return Msg.wParam;
}


BOOL CALLBACK DialogProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{

  switch(msg)  
  {  
	case WM_COMMAND:
		{
				switch(LOWORD(wParam))
				{
				case ID_CANCEL: 
					EndDialog(g_hDialog,0); break;
				case ID_OK:
					{ 
					UpdateWindow(hWnd);
					InvalidateRect(hWnd,NULL,TRUE);
					int cuv1,cuv2;
					GetDlgItemText(g_hDialog,IDC_EDIT1,sir,100);
					GetDlgItemText(g_hDialog,IDC_EDIT3,subsir,100);
					strcpy(S,sir);
					strcpy(s,subsir);
					cuv1=GetWindowTextLength(GetDlgItem(hWnd,IDC_EDIT1));
					if (cuv1==0)
					{
						MessageBox(hWnd,"Nu ati introdus un sir!!","ERROR!!!",MB_OK);
						break;
					}
					cuv2=GetWindowTextLength(GetDlgItem(hWnd,IDC_EDIT3));
					if (cuv2==0)
					{
						MessageBox(hWnd,"Nu ati introdus un subsir!!","EROOR!!!",MB_OK);
						break;
					}
					g_hdialog=CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG2), hWnd, DialogProc2);
					EndDialog(g_hDialog,0);
					ShowWindow(g_hdialog,SW_SHOW);
					break;
					}
				}
				break;
		}

    case WM_CLOSE:
		EndDialog(g_hDialog,0);
        break;  
	default: return FALSE;
  }
  return TRUE;

}


//---------------------------------------------------------------------------
LRESULT CALLBACK WndProcedure(HWND hWnd2, UINT Msg,
			   WPARAM wParam, LPARAM lParam)
{
    switch(Msg)
    {
		
    case WM_DESTROY:
        PostQuitMessage(WM_QUIT);
        break;
	case WM_CREATE:
		{
			MessageBox(hWnd,"      Algoritmul imaginat de Boyer si Moore reprezinta o tehnica avansata de cautare a unui sir de caractere intr-un sir de caractere sursa!!                                             In continuare va este prezentata o aplicatie windows 32 asupra algoritmului.","APPLICATION",MB_OK);
			break;
		}
		case WM_PAINT:
		{
			if(ok==1)
			{
				hdc=BeginPaint(hWnd2,&ps);
				DrawTable(hdc);
				char er[10];
				p=112;
				for(int i=0;i<n;i++)
					{
						Rectangle(hdc,rt[i]->left,rt[i]->top,rt[i]->right,rt[i]->bottom);
						er[0]=text[i];
						er[1]=NULL;
						TextOut(hdc,p,172,er,1);
						char cr[2];
						cr[0]=nr[i]+'0';
						cr[1]=NULL;
						TextOut(hdc,p,221,cr,1);
						p=p+30;
					}
				MoveToEx(hdc,rt[0]->left,rt[0]->top+((rt[0]->bottom-rt[0]->top)/2),NULL);
				LineTo(hdc,rt[n-1]->right,202);
				colorare(hdc,ps,41,89,254,150,100,352,130);
				colorare(hdc,ps,41,89,254,150,260,325,290);
				char tabel[45]="Tabel cu pasul fiecarui caracter";
				TextOut(hdc,150,100,tabel,strlen(tabel));
				afisare_sir(0);
				afisare_subsir(0);	
				boyer();
				HWND hWndButton=CreateWindowEx(NULL, 
			"BUTTON",
		"detalii...",
		WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON,
		30,
		100,
		100,
		24,
		hWnd2,
		(HMENU)IDC_MAIN_BUTTON,
		GetModuleHandle(NULL),
		NULL);
					HWND hWndButton2=CreateWindowEx(NULL, 
			"BUTTON",
		"*****",
		WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON,
		30,
		260,
		100,
		24,
		hWnd2,
		(HMENU)IDC_MAIN_BUTTON2,
		GetModuleHandle(NULL),
		NULL);
			ok=0;
			UpdateWindow(hWnd2);
				HWND hWndButton3=CreateWindowEx(NULL, 
			"BUTTON",
		"PERFORMANTA",
		WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON,
		30,
		50,
		150,
		24,
		hWnd2,
		(HMENU)IDC_MAIN_BUTTON3,
		GetModuleHandle(NULL),
		NULL);
			ok=0;
			EndPaint(hWnd2,&ps);
			UpdateWindow(hWnd2);
		}
		break;
		}
case WM_COMMAND:
            switch(LOWORD(wParam))
            {
				case ID_FILE_NEWW:
				{
					colorare(hdc,ps,41,89,254,150,260,200,220);
					g_hDialog=CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), hWnd2, DialogProc);
					ShowWindow(g_hDialog,SW_SHOW);
					break;
				}
				case IDC_MAIN_BUTTON:
				{
					g_hDialog2=CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG3), hWnd4,AboutDlgProc);
					ShowWindow(g_hDialog2,SW_SHOW);
					break;
				}
				case IDC_MAIN_BUTTON2:
				{
					MessageBox(hWnd,"       Indiferent unde a aparut nepotrivirea dintre sir si subsir, se ia in considerare caracterul din sir corespunzator ultimului caracter al subsirului","ATTENTION!!",MB_OK);
					break;
				}
				case IDC_MAIN_BUTTON3:
				{
					perform=CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG6), hWnd_p,AboutDlgPerform);
					ShowWindow(perform,SW_SHOW);
					break;
				}
                case ID_FILE_EXIT:
				{
					PostQuitMessage(WM_QUIT);
					break;
				}
				case ID_HELP_APP:

				{			
					g_hDialog4=CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG4), hWnd5,AboutDlgProc5);
					ShowWindow(g_hDialog4,SW_SHOW);
					break;
				}
				case ID_HELP_ALG:

				{			
					g_hDialog5=CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG5), hWnd6,AboutDlgProc6);
					ShowWindow(g_hDialog5,SW_SHOW);
					break;
				}
            }
    default:
        // Process the left-over messages
        return DefWindowProc(hWnd2, Msg, wParam, lParam);
}
    // If something was not done, let it go
    return 0;
}
//---------------------------------------------------------------------------

